/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import Networking.ChatMessage;
import Objects.World;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.font.BitmapText;
import com.jme3.math.ColorRGBA;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.scene.Node;
import com.jme3.ui.Picture;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.TextField;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.input.NiftyInputEvent;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import de.lessvoid.nifty.tools.SizeValue;
import de.lessvoid.xml.xpp3.Attributes;
import java.util.ArrayList;
import java.util.Properties;

/**
 *
 * @author 1906528
 */
public class GUI extends SimpleApplication implements ScreenController{
    private ArrayList<BitmapText> chatLog = new ArrayList<BitmapText>();
    private Picture pic; //Declares the GUI picture
    private BitmapText ch; //Declares the crosshairs text
    private NiftyJmeDisplay niftyDisplay; //Declares the object that holds the gui
    private static Nifty nifty; //Declares the nifty object
    private Element progressBarElement; //Declares the loading bar
    private TextRenderer textRenderer; //Declares a text renderer
    private int loadcount = 1, loaded = 0; //Declares ints that are used to control the game logic
    private Node worldNode;
    private World mainWorld;
    public GUI(){
        niftyDisplay = new NiftyJmeDisplay(assetManager, //Constructs the GUI display
                inputManager,
                audioRenderer,
                guiViewPort);
        nifty = niftyDisplay.getNifty(); //Sets the nifty
 
        nifty.fromXml("Interface/nifty_loading.xml", "start", this); //Loads the XML file
 
        guiViewPort.addProcessor(niftyDisplay); //Starts the nifty
        populateChatLog();
        worldNode = Main.getWorld().getRootNode();
        mainWorld = Main.getWorld();
    }

    @Override
    public void simpleInitApp() {
        
    }
    public void chatAdd(String input) {
        for(int i = 0; i < 34; i++){
            chatLog.get(i).setText(chatLog.get(i + 1).getText());
        }
        chatLog.get(34).setText(input);
    }
    public void populateChatLog(){
        for(int i = 0; i < 35; i++){
            chatLog.add(new BitmapText(guiFont, false));
            chatLog.get(i).setSize(guiFont.getCharSet().getRenderedSize());      // font size
            chatLog.get(i).setColor(ColorRGBA.White);                             // font color
            chatLog.get(i).setText("");             // the text
            chatLog.get(i).setLocalTranslation(0, settings.getHeight() - ((chatLog.get(i).getLineHeight() * i) + 5), 0); // position
            guiNode.attachChild(chatLog.get(i));
        }
    }
    public void bind(Nifty nifty, Screen screen) {
        progressBarElement = nifty.getScreen("loadlevel").findElementByName("progressbar");
    }

    public void onStartScreen() {
    }

    public void onEndScreen() {
    }

    public void bind(Nifty nifty, Screen screen, Element element, Properties parameter, Attributes controlDefinitionAttributes) {
        progressBarElement = element.findElementByName("progressbar");
    }

    public void init(Properties parameter, Attributes controlDefinitionAttributes) {
    }

    public void onFocus(boolean getFocus) {
    }

    public boolean inputEvent(NiftyInputEvent inputEvent) {
        return false;
    }
    
    public void setProgress(final float progress, String loadingText) {
        final int MIN_WIDTH = 32;
        int pixelWidth = (int) (MIN_WIDTH + (progressBarElement.getParent().getWidth() - MIN_WIDTH) * progress);
        progressBarElement.setConstraintWidth(new SizeValue(pixelWidth + "px"));
        progressBarElement.getParent().layoutElements();
        textRenderer.setText(loadingText);
    }
 
    public void showLoadingMenu() {
        Main.name = nifty.getCurrentScreen().findNiftyControl("input", TextField.class).getText();
        Main.send(Main.name + ": has entered the game");
        nifty.gotoScreen("loadlevel");
        loaded = 1;
        
    }
    public void loadStep(){
        if(loadcount == 1){
            Element element = nifty.getScreen("loadlevel").findElementByName("loadingtext");
            textRenderer = element.getRenderer(TextRenderer.class);
            mainWorld.bullets = new Node("Bullets");
            mainWorld.enemies = new Node("Enemies");
            mainWorld.playernode = new Node("Players");
            Main.getWorld().getRootNode().attachChild(Main.getWorld().bullets);
            worldNode.attachChild(mainWorld.enemies);
            rootNode.attachChild(Main.getWorld().playernode);
            ChatMessage join = new ChatMessage("join", "");
            join.setReliable(true);
            Main.client.send(join);
            setProgress(0.2f, "Loading Nodes");
        }
        else if(loadcount == 2){
            mainWorld.bas = new BulletAppState();
            mainWorld.bas.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
            stateManager.attach(mainWorld.bas);
            mainWorld.bas.getPhysicsSpace();
            mainWorld.bas.getPhysicsSpace().addCollisionListener(this);
            setProgress(0.4f, "Loading Physics");
        }
        else if(loadcount == 3){
            viewPort.setBackgroundColor(ColorRGBA.Black);
            flyCam.setMoveSpeed(100);
            setProgress(0.5f, "Loading Camera");
        }
        else if(loadcount == 4){
            Main.setUpKeys();
            Main.setUpLight();
            Main.setUpScene();
            setProgress(0.6f, "Creating Scene");
        }

        else if(loadcount == 5){
            mainWorld.setUpPlayer();
            setProgress(0.9f, "Loading Player");
        }
        else if(loadcount == 6){
            Main.initCannonBall();
            Main.initAudio();
            setProgress(0.8f, "Adding Effects");
        }
        else if(loadcount == 7){
            mainWorld.setUpEnemy();
            setProgress(1f, "Loading Enemies");
        }
        else if(loadcount == 8){
            inputManager.setCursorVisible(false);
            nifty.gotoScreen("end");
            nifty.getScreen("end").findNiftyControl("input", TextField.class).disable();
            nifty.getCurrentScreen().findElementByName("status").hide();
            loaded = 2;
        }
        loadcount++;
    }
}
